package com.frxuexi.mdm2204.view

import androidx.compose.foundation.Image
import androidx.compose.foundation.layout.padding
import androidx.compose.material3.NavigationBar
import androidx.compose.material3.NavigationBarItem
import androidx.compose.material3.Scaffold
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.res.painterResource
import androidx.navigation.NavDestination.Companion.hierarchy
import androidx.navigation.compose.NavHost
import androidx.navigation.compose.composable
import androidx.navigation.compose.currentBackStackEntryAsState
import androidx.navigation.compose.rememberNavController
import com.frxuexi.apps.view.AppsPage
import com.frxuexi.document.DocPage
import com.frxuexi.mdm2204.MyNavHost
import com.frxuexi.mdm2204.R
import com.frxuexi.mymvi.NewsPage

sealed class NavItem(val title: String, val img: Int, val route: String) {
    object MessagePage : NavItem("消息", R.drawable.icon1275, "message")
    object HomePage : NavItem("门户", R.drawable.icon64, "home")
    object MinePage : NavItem("我", R.drawable.icon64, "mine")
}

@Composable
fun MainPage() {
    val list = listOf<NavItem>(NavItem.MessagePage, NavItem.HomePage, NavItem.MinePage)
    val navController = rememberNavController()
    Scaffold(bottomBar = {
        val stackEntryState  by navController.currentBackStackEntryAsState()
        val destination = stackEntryState?.destination
        //二级页面不显示底部导航
        if(destination?.hierarchy?.any {
            list.map { navItem -> navItem.route }.contains(it.route)
            }==true) {
            NavigationBar {//底部导航组件
                list.forEach { nav ->
                    NavigationBarItem(selected = destination?.hierarchy?.any { it.route == nav.route } == true,
                        onClick = {
                            navController.navigate(nav.route) {
                                popUpTo(navController.graph.startDestinationId) {
                                    saveState = true //保持状态
                                }
                                launchSingleTop = true //栈顶复用
                                restoreState = true //恢复状态
                            }
                        },
                        icon = {
                            Image(
                                painter = painterResource(id = nav.img),
                                contentDescription = ""
                            )
                        },
                        label = { Text(text = nav.title) })
                }
            }
        }
    }) {
        MyNavHost(it = it, navController = navController)
    }
}